<?php

/**
 * user actions.
 *
 * @package    pendidikan indonesia
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      $id_user = $this->getUser()->getAttribute('app_user_id');
      $this->credent = $this->getUser()->getAttribute('credential');
      $this->user = Doctrine_Core::getTable('user')->getProfil($id_user);

      $id_sekolah = Doctrine_Core::getTable('user')->getIdSekolah($id_user);
      $this->sekolah = Doctrine_Core::getTable('sekolah')->getUserSekolah($id_sekolah);

      $this->konsultasi =  Doctrine_Core::getTable('konsultan')->getActiveUserKonsultan($id_user);
  }

  /*public function executeShow(sfWebRequest $request)
  {
    $this->user = Doctrine::getTable('user')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->user);
  }*/

  /*public function executeNew(sfWebRequest $request)
  {
    $this->form = new userForm();
  }*/

  /*public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new userForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }*/


  /*
   *
   * User Edit Profil
   *
   */
  public function executeEdit(sfWebRequest $request)
  {
      $this->form = new userForm(Doctrine::getTable('user')->find(array($this->getUser()->getAttribute('app_user_id'))));

      $this->processForm($request, $this->form);

  }

  public function executeUpdate(sfWebRequest $request)
  {
    
    $this->form = new userForm(Doctrine::getTable('user')->find(array($this->getUser()->getAttribute('app_user_id'))));

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  /*public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($user = Doctrine::getTable('user')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
    $user->delete();

    $this->redirect('user/index');
  }*/

  /*
   *
   * Sekolah
   *
   */
  public function executeSekolah(sfWebRequest $request) {
      $id_user = $this->getUser()->getAttribute('app_user_id');
      $id_sekolah = Doctrine_Core::getTable('user')->getIdSekolah($id_user);

      //$sekolah = Doctrine_Core::getTable('sekolah')->find(array($id_sekolah));

      $sekolah = Doctrine_Core::getTable('user')->getProfil($id_user);

      $this->form = new userSekolahForm($sekolah);

      $this->processSekolah($request, $this->form);
  }


  /*
   *
   * Registration
   *
   */
  public function executeRegister(sfWebRequest $request) {


      $this->userForm = new RegisterFormUser();
      $this->form = new RegisterForm();
      $this->userForm->embedForm('login', $this->form);

      $this->processRegister($request, $this->userForm);
  }


  /*
   *
   * Pertemanan
   *
   */
  public function executeTeman(sfWebRequest $request) {
      $this->teman = array();
  }

  public function executeCariteman(sfWebRequest $request) {
      if($request->isMethod('post')) {
          $param = $request->getPostParameter('search');
      }

      $this->teman = Doctrine_Core::getTable('user')->searchTeman($param);
  }

  public function executeTambahTeman(sfWebRequest $request) {
      
  }


  /*
   *
   * Konsultasi
   *
   */
  public function executeKonsultasi(sfWebRequest $request) {
      $id_user = $this->getUser()->getAttribute('app_user_id');
      $this->credential = $this->getUser()->getAttribute('credential');
      if($this->credential == 'guru') {
          $konsultanForm = new userKonsultanForm();
          $konsultanForm->setKonsultan($id_user);
          $this->konsultanForm = $konsultanForm;
          
          $this->konsultasi = Doctrine_Core::getTable('konsultasi')->getKonsultasiByKonsultan($id_user);
          
          $this->processKonsultan($request, $this->konsultanForm);

          $this->openKonsultan = Doctrine_Core::getTable('konsultan')->getActiveUserKonsultan($id_user);
          
          $this->setTemplate('konsultasiguru');
      } elseif($this->credential == 'siswa') {
          $form = new siswaKonsultasiForm();
          $form->setIdUser($id_user);

          $this->konsultasiForm = $form;

          $this->processKonsultasi($request, $this->konsultasiForm);

          $this->konsultasi = Doctrine_Core::getTable('konsultasi')->getKonsultasiByUser($id_user);
          
          $this->setTemplate('konsultasisiswa');
      }


  }

  public function executeKomentarkonsultasi(sfWebRequest $request) {
      $id_user = $this->getUser()->getAttribute('app_user_id');
      $this->credential = $this->getUser()->getAttribute('credential');

      if($this->credential == 'guru') {
          $id_konsultasi = $request->getParameter('id');
          $this->forward404Unless($this->konsultasi = Doctrine_Core::getTable('konsultasi')->getKonsultasiById($id_konsultasi));

          $this->komentar = Doctrine_Core::getTable('komentar_konsultasi')->getKomentarByKonsultasi($id_konsultasi);

          $komentarForm = new userKomentarKonsultasiForm();
          $komentarForm->setKonsultasiAndUser($id_konsultasi, $id_user);
          $this->komentarForm = $komentarForm;
          
      } elseif ($this->credential == 'siswa') {
          $id_konsultasi = $request->getParameter('id');
          $konsultasi = $this->getRoute()->getObject();
              
          $this->forward404Unless($this->konsultasi = Doctrine_Core::getTable('konsultasi')->getKonsultasiById($id_konsultasi));

          
          $this->komentar = Doctrine_Core::getTable('komentar_konsultasi')->getKomentarByKonsultasi($id_konsultasi);

          $komentarForm = new userKomentarKonsultasiForm();
          $komentarForm->setKonsultasiAndUser($id_konsultasi, $id_user);
          $this->komentarForm = $komentarForm;

      }    
  }

  public function executeKomentarkonsultasicreate(sfWebRequest $request) {
      $id_user = $this->getUser()->getAttribute('app_user_id');
      $this->credential = $this->getUser()->getAttribute('credential');

      if($this->credential == 'guru') {
          $komentar_konsultasi = $request->getPostParameter('komentar_konsultasi');
          $id_konsultasi = $komentar_konsultasi['id_konsultasi'];

          $komentarForm = new userKomentarKonsultasiForm();
          $komentarForm->setKonsultasiAndUser($id_konsultasi, $id_user);

          $this->komentarForm = $komentarForm;

          $this->konsultasi = Doctrine_Core::getTable('konsultasi')->getKonsultasiById($id_konsultasi);
          $this->komentar = Doctrine_Core::getTable('komentar_konsultasi')->getKomentarByKonsultasi($id_konsultasi);

          $this->processKomentarKonsultasi($request, $this->komentarForm, $this->konsultasi);
          $this->setTemplate('komentarkonsultasi');

      } elseif($this->credential == 'siswa') {
          $komentar_konsultasi = $request->getPostParameter('komentar_konsultasi');
          $id_konsultasi = $komentar_konsultasi['id_konsultasi'];
          
          $komentarForm = new userKomentarKonsultasiForm();
          $komentarForm->setKonsultasiAndUser($id_konsultasi, $id_user);
          
          $this->komentarForm = $komentarForm;

          $this->konsultasi = Doctrine_Core::getTable('konsultasi')->getKonsultasiById($id_konsultasi);
          $this->komentar = Doctrine_Core::getTable('komentar_konsultasi')->getKomentarByKonsultasi($id_konsultasi);

          $this->processKomentarKonsultasi($request, $this->komentarForm, $this->konsultasi);
          $this->setTemplate('komentarkonsultasi');
      }
  }


  /*
   *
   * Pesan
   *
   */
  public function executePesan(sfWebRequest $request) {
      $id_user = $this->getUser()->getAttribute('app_user_id');
      $this->pesan = Doctrine_Core::getTable('pesan')->getActiveUserPesan($id_user);

      $form = new userPesanForm();
      $form->setIdPengirim($id_user);
      $this->pesanForm = $form;


      $this->processPesan($request, $this->pesanForm);
  }



  /*
   *
   * Form Processing
   *
   */
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
      if($request->isMethod('put')) {
          $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
          if ($form->isValid())
          {
            $user = $form->save();

            $this->redirect('user/index');
          }
      }
    
  }

  protected function processRegister(sfWebRequest $request, sfForm $form) {
      if($request->isMethod('post')) {
        $form->bind($request->getParameter($form->getName()));

        if($form->isValid()) {
            $form->save()->getId();
            $this->redirect('user/register');
        }

      }
  }

  protected function processSekolah(sfWebRequest $request, sfForm $form) {
      if($request->isMethod('put')) {
          $form->bind($request->getParameter($form->getName()));

          if($form->isValid()) {
              $form->save();
              $this->redirect('user/index');
          }
      }
  }

  protected function processPesan(sfWebRequest $request, sfForm $form) {
      if($request->isMethod('post')) {
          $form->bind($request->getParameter($form->getName()));

          if($form->isValid()) {
              $form->save();
          }
      }
  }

  protected function processKonsultan(sfWebRequest $request, sfForm $form) {
      if($request->isMethod('post')) {
          $form->bind($request->getParameter($form->getName()));

          if($form->isValid()) {
              $form->save();
              $this->redirect('user/index');
          }
      }
  }

  protected function processKonsultasi(sfWebRequest $request, sfForm $form) {
      if($request->isMethod('post')) {
          $form->bind($request->getParameter($form->getName()));

          if($form->isValid()) {
              $form->save();
              $this->redirect('user/index');
          }
      }
  }

  protected function processKomentarKonsultasi(sfWebRequest $request, sfForm $form, $konsultasi) {
      if($request->isMethod('post')) {
          $form->bind($request->getParameter($form->getName()));

          if($form->isValid()) {
              $form->save();
              $this->redirect('komentar_konsultasi', $konsultasi);
          }
      }
  }
  
}